package com.xiaobug.demo.alipay.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.xiaobug.demo.R;
import com.xiaobug.demo.BR;
import com.xiaobug.demo.alipay.ItemBean;
import com.xiaobug.demo.alipay.ItemMoveCallbackImpl;
import com.xiaobug.demo.alipay.ItemMoveHelperListener;
import com.xiaobug.demo.alipay.MenuItem;
import com.xiaobug.demo.databinding.AlipayRvEditBinding;
import com.xiaobug.demo.databinding.AlipayRvGroupListBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 菜单分组列表的Adapter
 * Created by ljb on 2018/5/17.
 */

public class MenuRecyclerListAdapter extends RecyclerView.Adapter<BindingViewHolder> {

    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_NORMAL = 1;  //说明是不带有header

    private final LayoutInflater mInflater;
    private List<ItemBean> mItemData;
    private Context c;

    public MenuRecyclerListAdapter(Context context) {
        c = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<ItemBean> menuItemList) {
        if (mItemData == null)
            mItemData = new ArrayList<>();
        mItemData.clear();
        mItemData.addAll(menuItemList);
    }

    public List<ItemBean> geData() {
        return mItemData;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {//头布局
            return TYPE_HEADER;
        }
        return TYPE_NORMAL;
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER)//头布局
            return new BindingViewHolder<>(DataBindingUtil.inflate(mInflater, R.layout.alipay_rv_edit, parent, false));
        //分组布局
        return new BindingViewHolder<>(DataBindingUtil.inflate(mInflater, R.layout.alipay_rv_group_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final BindingViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == TYPE_HEADER) {//头布局
            AlipayRvEditBinding binding = (AlipayRvEditBinding) holder.getBinding();
            final MenuRecyclerviewGridEditAdapter menuRecyclerviewGridEditAdapter = new MenuRecyclerviewGridEditAdapter(c);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(c, 4) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            binding.tvGrid.setLayoutManager(gridLayoutManager);
            binding.tvGrid.setAdapter(menuRecyclerviewGridEditAdapter);
            menuRecyclerviewGridEditAdapter.setData(hearData);

             ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemMoveCallbackImpl(new ItemMoveHelperListener() {
                @Override
                public void onItemMoved(int fromPosition, int toPosition) {
                    Log.e("tag", "onItemMoved: " + fromPosition + "----------" + toPosition);
                    if (fromPosition < toPosition) {
                        for (int i = fromPosition; i < toPosition; i++) {
                            Collections.swap(hearData, i, i + 1);
                        }
                    } else {
                        for (int i = fromPosition; i > toPosition; i--) {
                            Collections.swap(hearData, i, i - 1);
                        }
                    }
//                    menuRecyclerviewGridEditAdapter.setData(hearData);
                }
            }));
            itemTouchHelper.attachToRecyclerView(binding.tvGrid);
            menuRecyclerviewGridEditAdapter.setItemTouchHelper(itemTouchHelper);


        } else {//列表
            ItemBean itemBean = mItemData.get(position - 1);
            AlipayRvGroupListBinding binding = (AlipayRvGroupListBinding) holder.getBinding();
            binding.setVariable(BR.item, mItemData.get(position - 1));
            MenuRecyclerviewGridAdapter menuRecyclerviewGridAdapter = new MenuRecyclerviewGridAdapter(c);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(c, 4) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            binding.tvGrid.setLayoutManager(gridLayoutManager);
            binding.tvGrid.setAdapter(menuRecyclerviewGridAdapter);
            menuRecyclerviewGridAdapter.setData(itemBean.menuItemList);

        }
    }

    @Override
    public int getItemCount() {
        return mItemData == null ? 1 : mItemData.size() + 1;
    }


    private List<MenuItem> hearData;


    public void setHeaderData(List<MenuItem> bean) {
        if (hearData == null)
            hearData = new ArrayList<>();
        hearData.clear();
        hearData.addAll(bean);
        notifyItemInserted(0);
    }

}
